package com.wenjie.shoprankmonitor;

import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import com.google.appengine.api.datastore.Key;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Key_monitor_list {
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
	
	@Persistent
	private Long key_id;
	
	@Persistent
	private String key_name;
	
	@Persistent
	private Long monitor_time;
	
	@Persistent
	private String message_content;
	
	@Persistent
	private User user;
	
	@Persistent(mappedBy = "monitor_key")
	private List<Shop_result> results;
	
	public Key_monitor_list(Long key_id, String key_name) {
		this.key_id = key_id;
		this.key_name = key_name;
		//monitor_time = new Date();
		monitor_time = new Date().getTime();
	}
	
	public Key getKey() {
		return key;
	}
	
	public Long getKey_id() {
		return key_id;
	}
	
	public void setKey_name(String key_name) {
		this.key_name = key_name;
	}
	
	public String getKey_name() {
		return key_name;
	}
	
	public void setMonitor_time() {
		this.monitor_time = new Date().getTime();
	}
	
	public Date getMonitor_time() {
		//return monitor_time;
		return new Date(monitor_time);
	}
	
	public Long getMonitor_timeAsLong() {
		return monitor_time;
	}
	
	public void setMessage_content(String message_content) {
		this.message_content = message_content;
	}
	
	public String getMessage_content() {
		return message_content;
	}
	
	public User getUser() {
		return user;
	}
	
	public List<Shop_result> getResults() {
		return results;
	}
	
	public void addResult(String default_url, String owner_nick, 
			String good_title, Long num_id, Long owner_score, int rank) {
		results.add(new Shop_result(default_url, owner_nick, good_title, num_id, owner_score, rank));
	}
	
	public void delResult(int index) {
		if(index < results.size()) {			
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Shop_result result = pm.getObjectById(Shop_result.class, results.get(index).getKey());
			pm.deletePersistent(result);
			pm.close();
		}
	}
}
